import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';
import 'package:study_demo/base/BaseController.dart';
import 'package:study_demo/ui/rename/picker.dart';
import 'package:path/path.dart' as path;

class RenameController extends BaseController {
  final filePath = ''.obs;
  final file = File('').obs;

  final imageList = <ImageFile>[].obs;

  TextEditingController _nameEditingController = TextEditingController();

  TextEditingController get nameEditingController => _nameEditingController;

  TextEditingController _indexTextEditingController = TextEditingController();

  TextEditingController get indexTextEditingController =>
      _indexTextEditingController;

  final pickController = MultiImagePickerController(
    maxImages: 100,
    images: <ImageFile>[], // array of pre/default selected images
    picker: (int pickCount, Object? params) async {
      return await pickImagesUsingFilePicker(true);
    },
  );

  Future<void> renameImageFile(String newName) async {
    outputDirectory('测试');
  }

  outputDirectory(String name) async {
    var saveFile = await FilePicker.platform.saveFile(
      fileName: name,
      type: FileType.image,
      lockParentWindow: true,
    );
    if (saveFile != null) {}
  }

  Future<void> renameFile(String oldName, String newName) async {
    final directory = await FilePicker.platform.pickFiles(type: FileType.image);
    filePath.value = directory!.paths[0]!;
  }

  reName() async {
    String name = _nameEditingController.text.toString();
    if (!name.isAlphabetOnly) {
      toast('名称只能为字母');
      return;
    }
    if (_indexTextEditingController.text.isEmpty) {
      toast('索引不能为空');
      return;
    }
    int index = int.parse(_indexTextEditingController.text.trim().toString());

    imageList.value = pickController.images.toList();
    if (imageList.isEmpty) {
      toast('请先添加图片');
      return;
    }
    for (int i = 0; i < imageList.length; i++) {
      print(imageList[i].path);
      String imagePath = imageList[i].path.toString();
      // imageList[i].path = await reNameFile(imagePath, index + i);
      print(imageList[i].path);
    }
    update();
  }

  Future<String> reNameFile(String imagePath, int index) async {
    final oldName = imagePath;

    // 分离文件路径的目录和文件名
    final directory = path.dirname(oldName);
    final fileName = path.basename(oldName);

    String fileNameWithoutExt = path.basenameWithoutExtension(fileName);

    String extension = path.extension(fileName);

    // 修改文件名，这里以添加"_renamed"为例
    final newFileName = "${_nameEditingController.text}_$index$extension";

    // 重新组合目录和新的文件名
    final newName = path.join(directory, newFileName);

    try {
      final file = File(oldName);
      if (await file.exists()) {
        await file.rename(newName);
        toast('操作成功');
        print('newName:$newName');
        return newName;
      } else {
        toast('文件不存在');
        return '';
      }
    } catch (e) {
      toast('操作失败');
      return '';
    }
  }

  fileLocation() {
    FilePicker.platform.pickFiles(initialDirectory: imageList[0].path);
  }
}
